<?php

/**
 * transactions actions.
 *
 * @package    budgetpulse
 * @subpackage transactions
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class transactionsActions extends BaseActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
  
  public function executeAdd(sfWebRequest $request) {
    $this->success = false;
    $this->message = "";
    
    $httpService = new HttpService();
    
    try {
      $type = $request->getParameter('type', 'Expense');
      $accountId = $request->getParameter('account_id');
      $description = $request->getParameter('description');
      $date = new DateTime($request->getParameter('date'));
      $amount = $request->getParameter('amount');
      $category = $request->getParameter('category');
      $tags = $request->getParameter('tags');
      
      // filter parameters
      if (!in_array($type, array('Expense', 'Income'))) $type = 'Expense';
      $tags = explode(',', $tags);
      foreach($tags as $k => $tag) {
        $tags[$k] = trim($tag);
      }
      
      if ($httpService->addTransaction($type, $accountId, $description, $date, $amount, $category, $tags)) {
        $this->success = true;
      }
      $error = true;
    }
    catch (Exception $e) {
      $this->success = false;
      $this->message = $e->getMessage();
    }
    
    return $this->renderJSON();
  }
}
